package com.blog.common;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.blog.admin.AdminController;
import com.blog.reception.ReceptionController;
import com.jfinal.config.*;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.blog.model._MappingKit;

public class Config extends JFinalConfig {

    // 配置文件和开发环境
    private static final boolean isDev = isDev();
    public static String CONFIG_FILE_NAME = "config.properties";

    static {
        if (isDev) CONFIG_FILE_NAME = "config_dev.properties";
    }

    private static Prop loadConfig() {
        return PropKit.use(CONFIG_FILE_NAME);
    }

    /**
     * 判断是否为开发环境
     */
    private static boolean isDev() {

        return true;
    }

    /**
     * 创建数据库链接
     */
    public static DruidPlugin createDruidPlugin() {

        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbc_url"), PropKit.get("jdbc_user"), PropKit.get("jdbc_password").trim());
        // StatFilter提供JDBC层的统计信息
        druidPlugin.addFilter(new StatFilter());
        // WallFilter的功能是防御SQL注入攻击
        WallFilter wallDefault = new WallFilter();
        druidPlugin.addFilter(wallDefault);
        druidPlugin.setInitialSize(PropKit.getInt("jdbc_initial_size"));
        druidPlugin.setMinIdle(PropKit.getInt("jdbc_min_idle"));
        druidPlugin.setMaxActive(PropKit.getInt("jdbc_max_active"));

        return druidPlugin;
    }

    @Override
    public void configConstant(Constants me) {

        // 设置开发模式
        me.setDevMode(isDev);

        // 加载配置文件
        PropKit.use(CONFIG_FILE_NAME);

        // 配置错误页面
        me.setError404View("/WEB-INF/view/common/error.html");
        me.setError500View("/WEB-INF/view/common/error.html");

        // 配置默认模板引擎
        me.setViewType(ViewType.FREE_MARKER);

    }


    @Override
    public void configRoute(Routes routes) {
        routes.setBaseViewPath("/WEB-INF/view");
        routes.add("/blog/reception", ReceptionController.class);
        routes.add("/blog/admin", AdminController.class);
    }

    @Override
    public void configPlugin(Plugins me) {

        // 数据库连接池插件
        DruidPlugin druidPlugin = createDruidPlugin();
        me.add(druidPlugin);

        // ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(true);
        me.add(arp);

        // 数据表映射
        _MappingKit.mapping(arp);
    }

    @Override
    public void configInterceptor(Interceptors me) {
        me.add(new WebInterceptor());
    }

    @Override
    public void configHandler(Handlers me) {
    }

    @Override
    public void configEngine(Engine me) {
        // 设置开发模式
        me.setDevMode(isDev);
        me.addSharedFunction("/WEB-INF/view/common/admin_logout.html");
        me.addSharedFunction("/WEB-INF/view/common/edit.html");
        me.addSharedFunction("/WEB-INF/view/common/editOne.html");
        me.addSharedFunction("/WEB-INF/view/common/layout.html");
    }
}
